#!/usr/bin/python
# -*- coding: utf-8 -*-
import ssl
import asyncio


from flask import request, jsonify
from flask_restful import Resource
from manager.models import GateKline, GateKlineSchema
from gate import gateManager
from manager import gateDao
# from gate.gateWebsocket import GateWebsocket

klines_schema = GateKlineSchema(many=True)
kline_schema = GateKlineSchema()


class GateKlineResource(Resource):

    def get(self, pair_name):
        num = request.args.get('num')
        endtime = request.args.get('endtime')
        if pair_name is not None:
            if num is not None and endtime is not None:
                print('+++++++++++++++  (pair_name, num, endtime) ')
                kline_list = gateDao.query_klines(pair_name, num, endtime)
            elif num is not None:
                print( '++++++++++++++  (pair_name=pair_name, num=num) ' )
                kline_list = gateDao.query_klines(pair_name=pair_name, num=num)
            elif endtime is not None:
                print('+++++++++++++++  (pair_name=pair_name, endtime=endtime) ')
                kline_list = gateDao.query_klines(pair_name=pair_name, endtime=endtime)
            else:
                print('+++++++++++++++  (pair_name) ')
                kline_list = gateDao.query_klines(pair_name)
            kline_list = klines_schema.dump(kline_list).data
            return {'status': 'success', 'data': kline_list}, 200
        else:
            return {'status': 'fail', 'message': '参数错误'}, 400



    def post(self, pair_name):


        if pair_name is not None:
            autosync = False
            json_data = request.get_json(force=True)
            if json_data is not None and json_data['autosync'] is not None:
                autosync = True if json_data['autosync']==1 else False

            last_kline = gateDao.query_last_kline(pair_name)

            kline_ws = ''#GateWebsocket()
            if last_kline is not None:
                # kline_ws.start_refresh_klines(pair_name=pair_name, start_time=last_kline.timestamp + 60,
                #                               autosync=autosync)
                #
                if kline_ws.start_refresh_klines(pair_name = pair_name, start_time=last_kline.timestamp + 60, autosync=autosync):
                    return {"status": 'success', 'message': 'ws 启动 - kline 刷新'}, 201
                else:
                    return {"status": 'fail', 'message': 'ws 启动失败 - kline 刷新'}, 400
            else:
                # kline_ws.start_refresh_klines(pair_name=pair_name, autosync=autosync)
                if kline_ws.start_refresh_klines(pair_name=pair_name, autosync=autosync):
                    return {"status": 'success', 'message': 'ws 启动 - kline 同步'}, 201
                else:
                    return {"status": 'fail', 'message': 'ws 启动失败 - kline 同步'}, 400
        else:
            return {"status": 'fail', 'message': '参数错误'}, 400



    def put(self, pair_name):
        json_data = request.get_json(force=True)
        if json_data is None or not 'pair_name' in json_data.keys():
            return {'message': '参数错误'}, 400

        if 'group_sec' in json_data.keys() and 'range_hour' in json_data.keys():
            kline_list = gateManager.get_kline_list(pair_name=json_data['pair_name'],
                                                    group_sec=json_data['group_sec'],
                                                    range_hour=json_data['range_hour'])
        elif 'group_sec' in json_data.keys():
            kline_list = gateManager.get_kline_list(pair_name=json_data['pair_name'],
                                                    group_sec=json_data['group_sec'])

        elif 'range_hour' in json_data.keys():
            kline_list = gateManager.get_kline_list(pair_name=json_data['pair_name'],
                                                    range_hour=json_data['range_hour'])
        else:
            kline_list = gateManager.get_kline_list(pair_name=json_data['pair_name'])

        if len(kline_list) == 0:
            return {'message': '查询失败'}, 400
        result = gateDao.insert_objects(kline_list)
        # result = gateDao.insertBatch(kline_list)
        return {"status": 'success', 'data': str(result) }, 201
